import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Elemento here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Elemento extends Actor
{
   
    private double d;
    private int xmundo;
    private int ymundo;
    private int ang;
    private double x;
    private double y;
    
    public Elemento()
    {
        d = -1;
        xmundo =0;
        ymundo =0;
    }
    
    public void inicia()
    {
        double num;

        xmundo = this.getWorld().getWidth()/2;
        ymundo = this.getWorld().getWidth()/2;
        x = xmundo-this.getX();
        y = ymundo-this.getY();
        if(x<0)
            x = x*-1;
        if(y<0)
            y = y*-1;
        d = Math.sqrt(x*x+y*y);
        num = y/x;
        ang = (int)(Math.toDegrees(Math.atan(num))); 
        if(this.getX()>=xmundo && this.getY()< ymundo)
            ang = 180-ang;
        if(this.getX()>xmundo && this.getY()>= ymundo)
            ang = 180+ang;
        if(this.getX()<=xmundo && this.getY()>ymundo)
            ang = 360 - ang;        
    }
    
    public void giraDer2(int ang2)
    {
        double xant = x;
        double yant = y;
        int aux;
        int auy;
        ang = ang+ang2;
        if(ang>359)
            ang = ang-360;
        x = d*Math.cos(Math.toRadians(ang));
        y = d*Math.sin(Math.toRadians(ang));
        if(x<0)
            x = x*-1;
        if(y<0)
            y = y*-1;
        aux = (int)(x-xant); 
        auy = (int)(y-yant);
        if(aux<0)
            aux = aux*-1;
        if(auy<0)
            auy = auy*-1;
      if(this.getX()<xmundo)
      {
            if(this.getY()<=ymundo)
                this.setLocation(this.getX()+aux,this.getY()-auy);
            if(this.getY()>ymundo)
                this.setLocation(this.getX()-aux,this.getY()-auy);
        }
        
       if(this.getX()>xmundo)
        {
            if(this.getY()<ymundo)
                this.setLocation(this.getX()+aux,this.getY()+auy);
            if(this.getY()>=ymundo)
                this.setLocation(this.getX()-aux,this.getY()+auy);
        }
        if(this.getX()==xmundo)
        {
            if(this.getY()<ymundo)
                this.setLocation(this.getX()+aux,this.getY()+auy);
            if(this.getY()>ymundo)
                this.setLocation(this.getX()-aux,this.getY()-auy);
        }
        //turn(ang2);
       
    }
  
    
    
     public void giraIzq(int ang2)
    {
        double xant = x;
        double yant = y;
        int aux;
        int auy;
        ang = ang-ang2;
        if(ang>359)
            ang = ang-360;
        x = d*Math.cos(Math.toRadians(ang));
        y = d*Math.sin(Math.toRadians(ang));
        if(x<0)
            x = x*-1;
        if(y<0)
            y = y*-1;
        aux = (int)(x-xant); 
        auy = (int)(y-yant);
        if(aux<0)
            aux = aux*-1;
        if(auy<0)
            auy = auy*-1;
      if(this.getX()<xmundo)
      {
            if(this.getY()<=ymundo)
                this.setLocation(this.getX()-aux,this.getY()+auy);
            if(this.getY()>ymundo)
                this.setLocation(this.getX()+aux,this.getY()+auy);
        }
        
       if(this.getX()>xmundo)
        {
            if(this.getY()<ymundo)
                this.setLocation(this.getX()-aux,this.getY()-auy);
            if(this.getY()>=ymundo)
                this.setLocation(this.getX()+aux,this.getY()-auy);
        }
        if(this.getX()==xmundo)
        {
            if(this.getY()<ymundo)
                this.setLocation(this.getX()-aux,this.getY()+auy);
            if(this.getY()>ymundo)
                this.setLocation(this.getX()+aux,this.getY()-auy);
        }
    }
    
    
    
    /**
     * Act - do whatever the prueba wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    public void act() 
    {
        
    }    
}

